/*
 * 变量实例管理 2010-01-15
 * author : tss
 * 
 */

//url 路径前缀
var startUrl = "/workflow/variableInst/";
var VariableInst = {

	id : "VariableInst",
	enterNewUrl : startUrl+"enterNew.html",
	enterEditUrl : startUrl+"enterEdit.html",
	enterViewUrl : startUrl+"enterView.html",
	saveUrl : startUrl + "save.html",
	updateUrl : startUrl + "update.html",
	removeUrl : startUrl + "delete.html",
	queryUrl : startUrl + "list.html",
	chkIds : "chkIds",
	form : function(){
	  return document.forms[0];
    },

	// 校验
	validate : function() {
		/*try {
			return Validator.Validate(VariableInst.form(), 3);
		} catch (e) {
			alert(e.message);
		}
		return false;*/
    	return true;
	},

	//首页
	desktop : function () {
	   
	  doSubmit(VariableInst.form(),initVar.desktopUrl);
	},
	
	// 查询
	search : function search() {
		doSubmit(VariableInst.form(), VariableInst.queryUrl);
	},
	
	// 高级查询
	adSearch : function search() {
		queryUpDown();
	},

	//进入新增
	enterNew : function() {

		var id = getSelectFirstId(VariableInst.chkIds);
		doSubmit(VariableInst.form(),VariableInst.enterNewUrl+'?id='+id);
	},
	
	//进入修改
	enterEdit : function(id) {
		
		doSubmit(VariableInst.form(), VariableInst.enterEditUrl+'?id='+id);
	},

	// 进入详细
	enterView : function(id) {

		doSubmit(VariableInst.form(), VariableInst.enterViewUrl + "?id=" + id);
	},

	// 删除
	remove : function() {

		commonRemove(VariableInst.form(), VariableInst.removeUrl, VariableInst.chkIds);
	},

	//保存
	save : function() {

		doSubmit(VariableInst.form(), VariableInst.saveUrl);
	},
	
	//修改
	update : function() {
		
		doSubmit(VariableInst.form(), VariableInst.updateUrl);
	},


	// 返回
	goBack : function() {

		doSubmit(VariableInst.form(), VariableInst.queryUrl+"?redirect=true");
	},
	
	//初始化下拉框
	initSelect : function(_url,_data,selectObj,currValue){
		
		currValue = currValue == "undefined" || currValue == null ? "" : currValue;
		var paramValue = selectObj.val();
		_data.act = "ajax";
		$.ajax({
			  type: "post",
			  url: initVar.basePath + startUrl + _url,
			  data:_data,
			  dataType: "json",
			  success: function(d){
				  fillSelect(d);
			  }
		}); 
		
		function fillSelect(d){
			
			selectObj.empty();
			$.each(d.ajax_data,function(i,item){
				
				var htmlStr = "<option value=\""+item.id+"\"" ;
				htmlStr +=	">"+item.name+"</option>";
				selectObj.append(htmlStr);
			});
			selectObj.val(currValue);
			
		}
	}
	
};

function getAttr(id,name){
	var obj = document.getElementById(id);
	if(obj != null && obj != "undefined"){
		return obj.getAttribute(name);
	}
	return null;
}

//加载
window.onload = function() {

	setTbStyle();
	execEvent(VariableInst,"INPUT","button");
	execEvent(VariableInst,"A");
	keyToSearch("search");
	$("#processDefId").bind("change",function(){
		VariableInst.initSelect("getProcessInstList.html",{"processDefId":$("#processDefId").val()},$("#processInstId"),getAttr("processInstId","value"));
		
		VariableInst.initSelect("getActivityList.html",{"processDefId":$("#processDefId").val()},$("#activityId"),getAttr("activityId","value"));
	});
	$("#processInstId").bind("change",function(){
		VariableInst.initSelect("getActivityInstList.html",{"processInstId":$("#processInstId").val()},$("#activityInstId"),getAttr("activityInstId","value"));
	});
	$("#activityId").bind("change",function(){
		VariableInst.initSelect("getActivityInstList.html",{"activityId":$("#activityId").val()},$("#activityInstId"),getAttr("activityInstId","value"));
	});
	
    VariableInst.initSelect("getProcessInstList.html",{"processDefId":$("#processInstId").val()},$("#processInstId"),getAttr("processInstId","value"));
    
    VariableInst.initSelect("getActivityList.html",{"processDefId":$("#activityId").val()},$("#activityId"),getAttr("activityId","value"));
    
    VariableInst.initSelect("getActivityInstList.html",{"processInstId":$("#activityInstId").val()},$("#activityInstId"),getAttr("activityInstId","value"));
    
	if(VariableInst.form().getAttribute("operate") != "newEditForm"){
		return;
	}
};
